package com.begye.smartloan.apptest.signinup.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.begye.smartloan.apptest.tool.IApiProcessCallback
import com.begye.smartloan.apptest.tool.LoginTool
import com.begye.smartloan.apptest.tool.doPostRequest
import com.data.analysislib.AnalyticsUtil
import org.json.JSONObject

class SignInUpViewModel : ViewModel() {

    var loginParam: String = ""
    var agreePrivacyPolicy: Boolean = true

    private val _canStartTheCountdownLiveData = MutableLiveData<Boolean>()
    val onCanStartTheCountdownLiveData get() = _canStartTheCountdownLiveData

    private val _loginSuccessfulLiveData = MutableLiveData<JSONObject>()
    val onLoginSuccessfulLiveData get() = _loginSuccessfulLiveData

    private val _loadingStateLiveData = MutableLiveData<Boolean>()
    val onLoadingStateLiveData get() = _loadingStateLiveData

    fun requestToSendVerificationCode(phone: String) {
        val params = hashMapOf<String, Any>().apply {
            put("mobile", phone)
        }
        doPostRequest(
            path = "/LxoZUj1z1Ryk5fKsw8hoxA",
            action = params,
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                    if (_loadingStateLiveData.value != true) {
                        _loadingStateLiveData.value = true
                    }
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                        loginParam = obj.optString("loginParam", "")

                        val directLoginFlag = obj.optString("directLoginFlag", "NO")
                        if (directLoginFlag == "YES") {
                            val code = "0000"
                            requestToLogin(
                                phone = phone,
                                code = code,
                                directLoginFlag = "YES"
                            )
                            return
                        }

                        _canStartTheCountdownLiveData.value = true

                    } catch (e: Exception) {
                        //
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                }
            })
    }

    fun requestToLogin(phone: String, code: String, directLoginFlag: String = "NO") {
        val params = hashMapOf<String, Any>().apply {
            put("mobile", phone)
            put("code", code)
            put("directLoginFlag", directLoginFlag)
            put("loginParam", loginParam)
        }
        doPostRequest(
            path = "/IsZC1Id0ebW02uTKfbBMg",
            action = params,
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                        LoginTool.updateLoginInfo(obj)
                        _loginSuccessfulLiveData.value = obj

                        AnalyticsUtil.addRegisterEvent()

                    } catch (e: Exception) {
                        //
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                }
            })
    }
}